#include "storage.hpp"

namespace storage {
    StorageProxy::StorageProxy(const std::string &file_name,const std::string &passwd) {
        this->file_name_ = file_name;
        this->passwd_ = passwd;
        storage_ = std::make_unique<storage_t>(init_storage(file_name));
        try {
        storage_->on_open = [this](sqlite3 *db) {
              sqlite3_key(db, this->passwd_.c_str(), this->passwd_.length());
            };
            storage_->sync_schema(true);
        } catch (std::exception &e) {
            LOG_INFO("数据库创建失败！")
        }
    }

    int StorageProxy::insert_info(const Student &student_info) const  {
        try {
            return storage_->insert(student_info);
        } catch (std::exception &e) {
            LOG_INFO("插入数据失败！")
            return 0;
        }
    }

    auto StorageProxy::get_info_by_studentId(long studentId) const {
        try {
            return storage_->get_all_pointer<Student>(
                sqlite_orm::where(sqlite_orm::c(&Student::studentId) == studentId));
        } catch (std::exception &e) {
            LOG_INFO("获取数据失败！")
            return std::vector<std::unique_ptr<Student> >();
        }
    }


    auto StorageProxy::get_all_pointer() const {
        try {
            return storage_->get_all_pointer<Student>();
        } catch (std::exception &e) {
            LOG_INFO("获取数据失败！")
            return std::vector<std::unique_ptr<Student> >();
        }
    }

    auto StorageProxy::delete_by_studentId(long studentId) const {
        try {
            return storage_->remove<Student>(studentId);
        } catch (std::exception &e) {
            LOG_INFO("删除数据失败！")
        }
    }
}
